// In the renderer process.
const {
  desktopCapturer
} = require('electron')


getScreenStream()
function getScreenStream() {
  desktopCapturer.getSources({
    types: ['screen']
  }).then(async function (sources) {
    for (const source of sources) {

      if (source.name === 'Entire Screen') {
        try {
          const stream = await navigator.mediaDevices.getUserMedia({
            audio: false,
            video: {
              mandatory: {
                chromeMediaSource: 'desktop',
                chromeMediaSourceId: source.id,

                maxWidth: window.screen.width,
                maxHeight: window.screen.height
              }
            }
          })
          handleStream(stream)
        } catch (e) {
          handleError(e, 1)
        }
        return
      }
    }
  })
}

function handleStream(stream) {
  const video = document.querySelector('#video')
  // console.log(video)
  video.srcObject = stream
  video.onloadedmetadata = (e) =>{
     video.play()
  }
}

function handleError(e) {
  console.log(e)
}
